package com.myblog.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.myblog.common.annotation.Log;
import com.myblog.common.core.controller.BaseController;
import com.myblog.common.core.domain.AjaxResult;
import com.myblog.common.enums.BusinessType;
import com.myblog.manage.domain.FriendLink;
import com.myblog.manage.service.IFriendLinkService;
import com.myblog.common.utils.poi.ExcelUtil;
import com.myblog.common.core.page.TableDataInfo;

/**
 * 友情链接Controller
 * 
 * @author mingo_sheep
 * @date 2025-05-02
 */
@RestController
@RequestMapping("/manage/link")
public class FriendLinkController extends BaseController
{
    @Autowired
    private IFriendLinkService friendLinkService;

    /**
     * 查询友情链接列表
     */
    @PreAuthorize("@ss.hasPermi('manage:link:list')")
    @GetMapping("/list")
    public TableDataInfo list(FriendLink friendLink)
    {
        startPage();
        // 如果 isApproved 有值，则按状态查询，否则查询全部
        List<FriendLink> list;
        if (friendLink.getIsApproved() != null) {
            list = friendLinkService.selectFreindLinkListByIsApproved(friendLink.getIsApproved());
        } else {
            list = friendLinkService.selectFriendLinkList(friendLink);
        }
        return getDataTable(list);
    }

    /**
     * 导出友情链接列表
     */
    @PreAuthorize("@ss.hasPermi('manage:link:export')")
    @Log(title = "友情链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FriendLink friendLink)
    {
        List<FriendLink> list = friendLinkService.selectFriendLinkList(friendLink);
        ExcelUtil<FriendLink> util = new ExcelUtil<FriendLink>(FriendLink.class);
        util.exportExcel(response, list, "友情链接数据");
    }

    /**
     * 获取友情链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:link:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(friendLinkService.selectFriendLinkById(id));
    }

    /**
     * 新增友情链接
     */
    @PreAuthorize("@ss.hasPermi('manage:link:add')")
    @Log(title = "友情链接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FriendLink friendLink)
    {
        return toAjax(friendLinkService.insertFriendLink(friendLink));
    }

    /**
     * 修改友情链接
     */
    @PreAuthorize("@ss.hasPermi('manage:link:edit')")
    @Log(title = "友情链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FriendLink friendLink)
    {
        return toAjax(friendLinkService.updateFriendLink(friendLink));
    }

    /**
     * 删除友情链接
     */
    @PreAuthorize("@ss.hasPermi('manage:link:remove')")
    @Log(title = "友情链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(friendLinkService.deleteFriendLinkByIds(ids));
    }
}
